float4x4 World;
float4x4 View;
float4x4 Projection;
float4x4 MatrixTransform : register(vs, c0);
// TODO: add effect parameters here.
texture Texture;
texture DepthTexture;
sampler2D Sampler;
float blurSize = 0.01;
sampler2D DepthSampler : register(s1) = sampler_state
{
	Texture = DepthTexture;
};
void SpriteVertexShader(inout float4 color    : COLOR0,  
                        inout float2 texCoord : TEXCOORD0,  
                        inout float4 position : SV_Position)  
{  
    position = mul(position, MatrixTransform);  
}  

float4 NormalDraw( float2 Tex : TEXCOORD0 ) : COLOR0
{
	return tex2D(Sampler,Tex.xy);
}
float4 blurX( float2 Tex : TEXCOORD0 ) : COLOR0
{
	float4 col = (float4)0;
	float div = 0;
	float4 blurSample = tex2D(DepthSampler, Tex);
	float blurAmount = blurSample.r + (1-blurSample.a);
	[branch] if (blurAmount > 0.01)
	{
		for (float i = -1; i <= 1; i+=0.1)
		{
			float4 b = tex2Dlod(DepthSampler, float4(Tex + float2( i * blurSize * blurAmount, 0),0,0));
			float4 tempCol = tex2Dlod(Sampler,float4(Tex + float2(i * blurSize * blurAmount * (b.r + (1 - b.a)),0),0, 0));
			float mul = pow((1- abs(i)) * (((tempCol.r + tempCol.g + tempCol.b)/3) * ((1 - b) * blurAmount) + 0.01),1);
			col+= tempCol*mul;
			div+=mul;
		}
		return col / div;
	}
	else return tex2D(Sampler,Tex.xy);

}
float4 blurY( float2 Tex : TEXCOORD0 ) : COLOR0
{
	float4 col = (float4)0;
	float div = 0;
	float4 blurSample = tex2D(DepthSampler, Tex);
	float blurAmount = blurSample.r + (1-blurSample.a);
	float blurAlpha = (blurAmount * blurSize) / (0.004);
	if (blurAlpha > 1)
	blurAlpha = 1;
	[branch] if (blurAmount > 0.01)
	{
		for (float i = -1; i <= 1; i+=0.1)
		{
			float4 b = tex2Dlod(DepthSampler, float4(Tex + float2( 0, i * blurSize * blurAmount),0,0));
			float4 tempCol = tex2Dlod(Sampler,float4(Tex.xy + float2(0, i * blurSize * blurAmount * (b.r + (1 - b.a))),0,0));
			float mul = pow((1- abs(i)) * (((tempCol.r + tempCol.g + tempCol.b)/3) * ((1 - b) * blurAmount) + 0.01),1);
			col+= tempCol * mul;
			div+=mul;
		}
	}
	return (col / div) * blurAlpha;
}
float4 bloomX( float2 Tex : TEXCOORD0 ) : COLOR0
{
	float4 col = (float4)0;
	float div = 0;
	for (float i = -1; i < 1; i+=0.2)
	{
		float b = tex2D(DepthSampler, Tex + float2(i * 0.01, 0)).g;
		col += tex2D(Sampler, Tex + float2(i * 0.01, 0)) * b;
		div ++;
	}
	return ((col) / div);
}
float4 blur(float2 Tex : TEXCOORD0):COLOR0
{
	float4 col = (float4)0;
	float div = 0;
	float4 b3 = tex2D(DepthSampler, Tex);
	float bb = b3.r + ((1 - b3.a));
	[branch] if (bb > 0.01)
	{
	for (float i = -1; i <= 1; i+=0.2)
	{
		for (float o = -1; o <= 1; o+=0.2)
		{
			[branch]if ((i * i) + (o * o) < 0.7)
			{
				float4 b2 = tex2Dlod(DepthSampler, float4(Tex.xy + float2(o * blurSize * bb, i * blurSize * 0.25),1,1));
				float b = b2.r + ((1 - b2.a));
				col+= tex2Dlod(Sampler,float4(Tex.xy + float2(o * blurSize * b * bb, i * blurSize * b * bb),1,1));
				div+= 1;
			}
		}
	}
	return col / div;
	}
	else return tex2D(Sampler,Tex.xy);
}
technique Normal
{
    pass Pass1
    {
        // TODO: set renderstates here.

        VertexShader = compile vs_2_0 SpriteVertexShader();
        PixelShader = compile ps_2_0 NormalDraw();
    }
}
technique Blur
{
    pass Pass1
    {
        // TODO: set renderstates here.

        VertexShader = compile vs_3_0 SpriteVertexShader();
        PixelShader = compile ps_3_0 blur();
    }
}
technique BlurX
{
    pass Pass1
    {
        // TODO: set renderstates here.

        VertexShader = compile vs_3_0 SpriteVertexShader();
        PixelShader = compile ps_3_0 blurX();
    }
}
technique BlurY
{
    pass Pass1
    {
        // TODO: set renderstates here.

        VertexShader = compile vs_3_0 SpriteVertexShader();
        PixelShader = compile ps_3_0 blurY();
    }
}
technique BloomX
{
	pass Pass1
    {
        // TODO: set renderstates here.

        VertexShader = compile vs_3_0 SpriteVertexShader();
        PixelShader = compile ps_3_0 bloomX();
    }
}
